﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using IToursDataService.Models;
using System.Data;
using IToursDataService;

namespace ToursDataService
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码、svc 和配置文件中的类名“TrainOrderCtrl”。
    public class TrainOrderCtrl : ITrainOrderCtrl
    {
        /// <summary>
        /// 创建飞机预订及费用数据
        /// </summary>
        /// <param name="expense"></param>
        /// <param name="flightOrder"></param>
        /// <returns></returns>
        public int CreateTrainOrderAndExpenseData(ExpenseEntity expense, TrainOrderEntity trainOrder)
        {
            Database db = DatabaseFactory.CreateDatabase();

            string sqlExpense = string.Format(@"INSERT INTO fee_Expense(
                                        [TourgroupID],
                                        [ProviderID],
                                        [ProviderName],
                                        [ServiceName],
                                        [Budget],
                                        [PayTotal],
                                        [ApplyPayTotal],
                                        [AdjustAmount],
                                        [Status],
                                        [TypeBelong],
                                        [ApplyPay],[PayStatus],[TourgroupName],[CreateByUserName],[TourGroupScheduleID],[CreateTime],[ExpenseTypeID],[ExpenseType])
                                        VALUES('{0}', {1}, '{2}', '{3}',{4},{5}, {6},{7}, '{8}', '{9}',{10},'{11}','{12}','{13}',{14},'{15}',{16},'{17}');Select SCOPE_IDENTITY();",
                                        expense.TourgroupID,
                                        expense.ProviderID,
                                        expense.ProviderName,
                                        expense.ServiceName,
                                        expense.Budget,
                                        expense.PayTotal,
                                        expense.ApplyPayTotal,
                                        expense.AdjustAmount,
                                        expense.Status,
                                        expense.TypeBelong,
                                        expense.ApplyPay,
                                        expense.PayStatus,
                                        expense.TourgroupName,
                                        expense.CreateByUserName,
                                        expense.TourGroupScheduleID,
                                        expense.CreateTime,
                                        expense.ExpenseTypeID,
                                        expense.ExpenseType);
            string expenseID = db.ExecuteScalar(CommandType.Text, sqlExpense).ToString();
            string sqlFlight = string.Format(@"INSERT INTO fee_TrainOrder ( [ExpenseID], [TourgroupID], [TourgroupName], [TourGroupScheduleID], [TrainCode],
                                                                            [StartTime], [EndTime], [BeginPosition], [EndPosition],
                                                                            [HardSleeperTopQuantity], [HardSleeperMiddleQuantity], [HardSleeperBottomQuantity],
                                                                            [SoftSleeperTopQuantity], [SoftSleeperBottomQuantity], [HighBoxQuantity], [HardSeatQuantity],
                                                                            [SoftSeatQuantity], [HardSleeperTopPrice], [HardSleeperMiddlePrice], [HardSleeperBottomPrice],
                                                                            [SoftSleeperTopPrice], [SoftSleeperBottomPrice], [HighBoxPrice], [HardSeatPrice], [SoftSeatPrice], 
                                                                            [HandlingCharge], [Remark],[StartDate],[EndDate],[OrderState],[AccompanyNum],[AdultNum],[ChildNum],[ChildSeatQuantity],[ChildSeatPrice],[ProviderID],[ProviderName],[Seat])
                                                                            VALUES({0},'{1}','{2}',{3},'{4}','{5}','{6}','{7}','{8}',{9},{10},{11},{12},{13},{14},{15},{16},{17},{18},{19},{20},{21},{22},{23},{24},{25},'{26}','{27}','{28}','{29}',{30},{31},{32},{33},{34},{35},'{36}','{37}')",
                                                                           expenseID,
                                                                           expense.TourgroupID,
                                                                           trainOrder.TourgroupName,
                                                                           trainOrder.TourGroupScheduleID,
                                                                           trainOrder.TrainCode,
                                                                           trainOrder.StartTime,
                                                                           trainOrder.EndTime,
                                                                           trainOrder.BeginPosition,
                                                                           trainOrder.EndPosition,
                                                                           trainOrder.HardSleeperTopQuantity,
                                                                           trainOrder.HardSleeperMiddleQuantity,
                                                                           trainOrder.HardSleeperBottomQuantity,
                                                                           trainOrder.SoftSleeperTopQuantity,
                                                                           trainOrder.SoftSleeperBottomQuantity,
                                                                           trainOrder.HighBoxQuantity,
                                                                           trainOrder.HardSeatQuantity,
                                                                           trainOrder.SoftSeatQuantity,
                                                                           trainOrder.HardSleeperTopPrice,
                                                                           trainOrder.HardSleeperMiddlePrice,
                                                                           trainOrder.HardSleeperBottomPrice,
                                                                           trainOrder.SoftSleeperTopPrice,
                                                                           trainOrder.SoftSleeperBottomPrice,
                                                                           trainOrder.HighBoxPrice,
                                                                           trainOrder.HardSeatPrice,
                                                                           trainOrder.SoftSeatPrice,
                                                                           trainOrder.HandlingCharge,
                                                                           trainOrder.Remark,
                                                                           trainOrder.StartDate,
                                                                           trainOrder.EndDate,
                                                                           trainOrder.OrderState,
                                                                           trainOrder.AccompanyNum,
                                                                           trainOrder.AdultNum,
                                                                           trainOrder.ChildNum,
                                                                           trainOrder.ChildSeatQuantity,
                                                                           trainOrder.ChildSeatPrice,
                                                                           trainOrder.ProviderID,
                                                                           trainOrder.ProviderName,
                                                                           trainOrder.Seat
                                                                           );
            return db.ExecuteNonQuery(CommandType.Text, sqlFlight);
        }
    }
}
